﻿using System.Collections.Generic;
using System.Web.Mvc;
using ORM.Areas.Admin.Controllers;
using ORM.Areas.Admin.ViewModels.ManageApproval;
using ORM.Business.Interfaces;
using ORM.Business.Models;
using ORM.Business.Models.Entities;
using ORM.Common;
using ORM.Framework.Filters;
using ORM.Resources;
using ORM.Web.Models;

namespace ORM.Areas.Admin.Controllers
{
    /// <summary>
    /// Handles categories management pages
    /// </summary>
    [PermissionAuthorize(Permissions = KeyObject.Permission.ManageApproval)]
    public class ManageApprovalController : AdminControllerBase
    {
        private IAnswerRepository _iAnswerRepository;
        private IGuideRepository _iGuideRepository;

        private OrmEntities ORMDbContext = new OrmEntities();
        public ManageApprovalController(IAnswerRepository iAnswerRepository, IGuideRepository iGuideRepository)
        {
            _iAnswerRepository = iAnswerRepository;
            _iGuideRepository = iGuideRepository;
        }

        public ActionResult QuestionApprovalIndex()
        {
            IList<Answer> answersList = new List<Answer>();

            var feedback = _iAnswerRepository.GetAllUnapprovedAnswers();

            //Set default value for Total record
            ViewData["Total"] = 0;

            if (feedback.Success)
            {
                answersList = feedback.Data;
                //ViewData["Total"] = feedback.Total;
            }
            else
            {
                SetMessage(feedback.Message, MessageType.Error);
            }

            return View(answersList);
        }
       

        public ActionResult AnswerDetails(int id)
        {

            AnswerDetailsViewModel vm = new AnswerDetailsViewModel();
            vm.Answer = ORMDbContext.Answers.Find(id);
            vm.Question = ORMDbContext.Questions.Find(vm.Answer.QuestionId);
            return PartialView(vm);
        }
        public ActionResult GuideDetails(int id)
        {

            GuideDetailsViewModel vm = new GuideDetailsViewModel();
            vm.Guide = ORMDbContext.Guides.Find(id);
            return PartialView(vm);
        }



        public ActionResult ApprovedAnswersIndex()
        {
            IList<Answer> answersList = new List<Answer>();

            var feedback = _iAnswerRepository.GetAllApprovedAnswers();

            //Set default value for Total record
            ViewData["Total"] = 0;

            if (feedback.Success)
            {
                answersList = feedback.Data;
                //ViewData["Total"] = feedback.Total;
            }
            else
            {
                SetMessage(feedback.Message, MessageType.Error);
            }

            return View(answersList);
        }
        

        public ActionResult Approve(int id)
        {
            var feedback = _iAnswerRepository.ApproveAnswer(id);

            if (feedback.Success)
            {
                SetMessage(Messages.ApproveAnswerSuccess, MessageType.Success);
            }
            else
            {
                SetMessage(feedback.Message, MessageType.Error);
            }

            return RedirectToAction("QuestionApprovalIndex");
        }
        public ActionResult Disapprove(int id)
        {
            var feedback = _iAnswerRepository.DisapproveAnswer(id);

            if (feedback.Success)
            {
                SetMessage(Messages.DisapproveAnswerSuccess, MessageType.Success);
            }
            else
            {
                SetMessage(feedback.Message, MessageType.Error);
            }

            return RedirectToAction("QuestionApprovalIndex");
        }
        public ActionResult ApproveGuide(int id)
        {
            var feedback = _iGuideRepository.ApproveGuide(id);

            if (feedback.Success)
            {
                SetMessage(Messages.ApproveGuideSuccess, MessageType.Success);
            }
            else
            {
                SetMessage(feedback.Message, MessageType.Error);
            }

            return RedirectToAction("GuideUnapprovalIndex");
        }
        public ActionResult DisApproveGuide(int id)
        {
            var feedback = _iGuideRepository.DisApproveGuide(id);

            if (feedback.Success)
            {
               // SetMessage(Messages.DisApproveGuideSuccess, MessageType.Success);
                SetMessage(Messages.DisApproveGuideSuccess, MessageType.Success);
            }
            else
            {
                SetMessage(feedback.Message, MessageType.Error);
            }

            return RedirectToAction("GuideUnapprovalIndex");
        }

        public ActionResult UnapproveGuide(int id)
        {
            var feedback = _iGuideRepository.UnapproveGuide(id);

            if (feedback.Success)
            {
                SetMessage(Messages.UnapproveGuideSuccess, MessageType.Success);
            }
            else
            {
                SetMessage(feedback.Message, MessageType.Error);
            }

            return RedirectToAction("GuideUnapprovalIndex");
        }

        public ActionResult GuideUnapprovalIndex()
        {
            IList<Guide> guidesList = new List<Guide>();

            var feedback = _iGuideRepository.GetAllUnapprovedGuides();

            //Set default value for Total record
            ViewData["Total"] = 0;

            if (feedback.Success)
            {
                guidesList = feedback.Data;
                //ViewData["Total"] = feedback.Total;
            }
            else
            {
                SetMessage(feedback.Message, MessageType.Error);
            }

            return View(guidesList);
        }
        public ActionResult GuideApprovalIndex()
        {
            IList<Guide> guidesList = new List<Guide>();

            var feedback = _iGuideRepository.GetAllApprovedGuides();

            //Set default value for Total record
            ViewData["Total"] = 0;

            if (feedback.Success)
            {
                guidesList = feedback.Data;
                //ViewData["Total"] = feedback.Total;
            }
            else
            {
                SetMessage(feedback.Message, MessageType.Error);
            }

            return View(guidesList);
        }

    }
}
